package swings.frames;

import swings.others.CommonJTextField;
import swings.panels.RecordPanel;
import utils.CommonUtils;
import utils.DataManageUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title: 新增记录frame
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class AddRecordFrame extends JFrame {

    private Container contentPane = this.getContentPane();
    private JPanel titlePanel = new JPanel();
    private JPanel urlPanel = new JPanel();
    private JPanel jrbPanel = new JPanel();
    private JLabel titleLabel = new JLabel("Title  :", JLabel.LEFT);
    private JLabel urlLabel = new JLabel("version:", JLabel.LEFT);
    private JLabel jrbLabel = new JLabel("Method:", JLabel.LEFT);
    private CommonJTextField titleField = new CommonJTextField(20);
    private CommonJTextField urlField = new CommonJTextField(20);
    private JRadioButton getJrb = new JRadioButton("GET");
    private JRadioButton postJrb = new JRadioButton("POST");
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JButton saveBtn = new JButton("Save");
    private Color bgColor = CommonUtils.bgColor;
    private Color inputColor = CommonUtils.inputColor;
    private Font font = CommonUtils.getFont();
    private RecordPanel recordPanel;
    private JFileChooser jfc = new JFileChooser(new File("."));

    /**
     * 构造方法
     */
    public AddRecordFrame(RecordPanel recordPanel) {
        this.recordPanel = recordPanel;
        //初始化组件
        initComponent();
        //初始化按钮
        initBtn();
        //初始化窗口
        init();
    }

    /**
     * 初始化组件
     */
    private void initComponent(){
        contentPane.setLayout(null);
        titlePanel.setBounds(0,5,390,40);
        urlPanel.setBounds(0,50,390,40);
        jrbPanel.setBounds(0,95,390,40);
        jrbPanel.setLayout(null);
        titleLabel.setFont(font);
        urlLabel.setFont(font);
        jrbLabel.setFont(font);
        titleField.setFont(font);
        urlField.setFont(font);
        titleLabel.setForeground(Color.WHITE);
        urlLabel.setForeground(Color.WHITE);
        jrbLabel.setForeground(Color.WHITE);
        titlePanel.setBackground(bgColor);
        urlPanel.setBackground(bgColor);
        jrbPanel.setBackground(bgColor);
        titleField.setBackground(inputColor);
        urlField.setBackground(inputColor);
        titleField.setForeground(Color.WHITE);
        urlField.setForeground(Color.WHITE);
        jrbPanel.setForeground(Color.WHITE);
        getJrb.setBackground(bgColor);
        postJrb.setBackground(bgColor);
        getJrb.setForeground(Color.WHITE);
        postJrb.setForeground(Color.WHITE);
        getJrb.setFont(font);
        postJrb.setFont(font);
        jrbLabel.setBounds(23,0,100,40);
        getJrb.setBounds(150,0,80,40);
        postJrb.setBounds(260,0,80,40);
        titlePanel.add(titleLabel);
        titlePanel.add(titleField);
        urlPanel.add(urlLabel);
        urlPanel.add(urlField);
        jrbPanel.add(jrbLabel);
        jrbPanel.add(getJrb);
        jrbPanel.add(postJrb);
        buttonGroup.add(getJrb);
        buttonGroup.add(postJrb);
        saveBtn.setBounds(160,140,70,30);
        saveBtn.setFont(font);
        //添加组件
        contentPane.add(titlePanel);
        contentPane.add(urlPanel);
        contentPane.add(jrbPanel);
        contentPane.add(saveBtn);
        contentPane.setBackground(bgColor);
    }

    /**
     * 初始化按钮
     */
    private void initBtn(){
        JButton chooseBtn = new JButton("选择文件");
        chooseBtn.setIcon(new ImageIcon("resources/image/info.png"));
        chooseBtn.setFont(new Font("微软雅黑", Font.BOLD, 12));
        chooseBtn.setForeground(Color.WHITE);
        chooseBtn.setVerticalTextPosition(JLabel.CENTER);
        chooseBtn.setHorizontalTextPosition(JLabel.CENTER);
        chooseBtn.setBounds(35,740,150,30);
        chooseBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openFileChooser();
            }
        });

        //保存按钮监听器
        saveBtn.addActionListener(e -> {
            //获取数据
            String title = titleField.getText();
            String url = urlField.getText();
            String method = getJrb.isSelected()?getJrb.getText():postJrb.getText();
            //生成id
            String id = CommonUtils.getRandom(5);
            //组装参数map
            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put("id",id);
            valueMap.put("title",title);
            valueMap.put("url",url);
            valueMap.put("method",method);
            valueMap.put("isEncrypt","");
            valueMap.put("dataSecret","");
            valueMap.put("dataSecretIV","");
            valueMap.put("appSecret","");
            valueMap.put("body","");
            valueMap.put("result","");
            valueMap.put("paramkey","");
            valueMap.put("paramvalue","");
            valueMap.put("paramdescription","");
            valueMap.put("headerkey","Content-Type");
            valueMap.put("headervalue","application/json;chert=UTF-8");
            valueMap.put("headerdescription","");
            valueMap.put("templatekey","");
            valueMap.put("templatevalue","");
            valueMap.put("templatedescription","");
            try {
                //插入数据
                Connection connectionNet = DataManageUtils.getConnection();
                Statement statementNet = connectionNet.createStatement();
                DataManageUtils.insertRecord(statementNet, valueMap);
                DataManageUtils.insertHeader(statementNet, valueMap);
                DataManageUtils.insertParam(statementNet, valueMap);
                DataManageUtils.insertTemplate(statementNet, valueMap);
                statementNet.close();
                connectionNet.close();
                recordPanel.initRecords("");
                recordPanel.recordMap.get(Integer.parseInt(id)).choose();
            } catch (ClassNotFoundException | SQLException | IOException ex) {
                ex.printStackTrace();
            }
            cleanFrame();
            dispose();
        });

    }

    public void init(){
        this.setTitle("NewService");
        this.setIconImage(ImageUtils.logo);
        this.setLocation((CommonUtils.screenWidth-400)/2, (CommonUtils.screenHeight-220)/2);
        this.setSize(400,220);
        this.setVisible(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * 清空jframe
     */
    private void cleanFrame(){
        titleField.setText("");
        urlField.setText("");
        buttonGroup.clearSelection();
    }

    public void openFileChooser(){
        //打开文件选择器对话框
        int status = jfc.showOpenDialog(this);
        //没有选打开按钮结果提示
        if(status!=JFileChooser.APPROVE_OPTION){
//            ta.setText("没有选中文件");
        } else{
            //被选中的文件保存为文件对象
            File file = jfc.getSelectedFile();
            file.getAbsolutePath();
        }
    }

}
